package cz.destil.settleup.gui;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MultiChoiceArrayAdapter extends ArrayAdapter<MultiChoiceItem> {
    private Context                            context;
    private int                                resource;
    private int                                textViewResourceId;
    private Map<Serializable, MultiChoiceItem> changedItems;
    private String                             hint;

    public String getHint() {
        return hint;
    }

    public void setHint(String hint) {
        this.hint = hint;
    }

    public MultiChoiceArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        this.context = context;
        this.textViewResourceId = textViewResourceId;
    }

    public MultiChoiceArrayAdapter(Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
    }

    public MultiChoiceArrayAdapter(Context context, int resource, int textViewResourceId, List<MultiChoiceItem> objects) {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
    }

    public MultiChoiceArrayAdapter(Context context, int resource, int textViewResourceId, MultiChoiceItem[] objects) {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
    }

    public MultiChoiceArrayAdapter(Context context, int textViewResourceId, List<MultiChoiceItem> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.textViewResourceId = textViewResourceId;
    }

    public MultiChoiceArrayAdapter(Context context, int textViewResourceId, MultiChoiceItem[] objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.textViewResourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View result = null;
        if (null == convertView) {
            LayoutInflater inflater = LayoutInflater.from(context);
            result = inflater.inflate(this.resource, parent, false);
            TextView textView = (TextView) result.findViewById(this.textViewResourceId);
            textView.setHint(this.getHint());
            textView.setText(this.getText());
        } else {
            result = super.getView(position, convertView, parent);
        }
        return result;
    }

    public Map<Serializable, MultiChoiceItem> getChangedItems() {
        return changedItems;
    }

    public void setItemAtIndexChecked(int position, boolean isChecked) {
        MultiChoiceItem changedItem = getItem(position);
        changedItem.setIsChecked(isChecked);
        if (null == changedItems) {
            changedItems = new HashMap<Serializable, MultiChoiceItem>();
        }
        changedItems.put(changedItem.userInfo(), changedItem);
        notifyDataSetChanged();
    }

    private String getText() {
        StringBuilder sb = new StringBuilder();
        int count = getCount();
        if (0 != count) {
            boolean hasComma = false;
            for (int i = 0; i < count; i++) {
                MultiChoiceItem item = getItem(i);
                if (item.isChecked()) {
                    sb.append((hasComma ? "," : "") + item.text());
                    hasComma = true;
                }
            }
        }
        return sb.toString();
    }
}
